package cn.xiangyuanpin.service.impl;

import cn.xiangyuanpin.dao.OrderDao;
import cn.xiangyuanpin.dao.OrderItemDao;
import cn.xiangyuanpin.dao.impl.OrderDaoImpl;
import cn.xiangyuanpin.dao.impl.OrderItemDaoImpl;
import cn.xiangyuanpin.pojo.Cart;
import cn.xiangyuanpin.pojo.CartItem;
import cn.xiangyuanpin.pojo.Order;
import cn.xiangyuanpin.pojo.OrderItem;
import cn.xiangyuanpin.service.OrderService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    private OrderItemDao orderItemDao=new OrderItemDaoImpl();
    private OrderDao orderDao=new OrderDaoImpl();


    @Override
    public String createOrder(Cart cart, Integer userId) {


        String orderId=System.currentTimeMillis()+""+userId;
        orderDao.saveOrder(new Order(orderId,new Date(),cart.getTotalPrice(),0,userId));

        for(Map.Entry<Integer, CartItem> entry:cart.getItems().entrySet()){
            CartItem cartItem=entry.getValue();
                   orderItemDao.saveOrderItem
                           (new OrderItem(null, cartItem.getName(), cartItem.getCount(),cartItem.getPrice(),
                           cartItem.getTotalPrice(),orderId));
        }
       cart.clear();
        return  orderId;
    }
}
